package portal.conf;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import portal.common.MyStringUtils;
import portal.conf.entity.BaseConf;
import portal.conf.entity.EtcHostsEntity;
@Component
@Scope("singleton")
public class EtcHostsConf extends AbstractConf {
	public EtcHostsConf (){
		setFileName("/etc/hosts");
	}
   
	@Override
	@Value("${com.etchostfile}")
	public void setFileName(String fname){
		super.setFileName(fname);
	}
	@Override
	protected BaseConf analysiConf(String tempString, int line, Class<?> t, BaseConf qe) {
		//if(qe==null)
		tempString=MyStringUtils.strictSpace(tempString);
		Method method;
		try {
			qe=(BaseConf) t.newInstance();
			qe.setLineEnd(line);
			qe.setLineStart(line);
			method = Class.forName(t.getName()).getMethod("setValue",String.class);
			method.invoke(qe,MyStringUtils.trimAll(tempString));
			addToConfList(qe);
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return null;
	}
	protected boolean isEntityExist(String key) {
		if(key==null|| MyStringUtils.isEmpty(key))return false;
		// TODO Auto-generated method stub
		if(findConfByName(key)!=null)
			return true;
		return false;
	}

	@Override
	protected boolean isConfStart(String line) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected boolean isConfEnd(String line) {
		// TODO Auto-generated method stub
		return false;
	}
	public List<BaseConf> selectAllConf(){
		return selectAllConf(EtcHostsEntity.class);
	}
	public void insertConf(EtcHostsEntity etcHosts){
		if(!isEntityExist(etcHosts))
			insertConf(etcHosts.toString().split("#"));
	}
	public void insertConf(String hostName,String ip,String dName){
		insertConf(new EtcHostsEntity(hostName,ip,dName));
	}
	public void insertConf(String hostLine){
		EtcHostsEntity ehe=new EtcHostsEntity();
		insertConf(ehe.setValue(hostLine));
	
	}
	private void insertConf(String[]data){
		super.insertConf(fileName, data);
		super.selectAllConf(new File(fileName), EtcHostsEntity.class);
	}
	public boolean removeConf(String name){
		boolean ret =removeConf(findConfByName(name));
		selectAllConf(new File(fileName), EtcHostsEntity.class);
		return ret;
	}
	public static void main(String[] args){
		EtcHostsConf conf= new EtcHostsConf();
		List<BaseConf> list =conf.selectAllConf();
		conf.insertConf("192.168.1.111 sgegege ssssssss");
		for(int index=0;index<list.size();index++){
			System.out.println(list.get(index));
		}
	}
}
